function showDiv(mask) {
	var mask = document.getElementById(mask);
	mask.style.display = "";
}
function hiddenDiv(mask) {
	var mask = document.getElementById(mask);
	mask.style.display = "none";
}
function sign(iid) {
	var ids = document.getElementsByName('merId');
	for ( var i = 0; i < ids.length; i++) {
		var id = ids.item(i);
		if (id.value == iid) {
			if (id.checked == true) {
				id.checked = false;
			} else {
				id.checked = true;
			}
			break;
		}
	}
}
function callback(data) {
	if ("" != data.lenght) {
		showDiv('mask');
	}
	var str = "";
	for ( var i = 0; i < data.length; i++) {
		// 动态的改变页面中div节点中的内容
		str = str
				+ '<tr onclick="sign('
				+ data[i].merchandiseId
				+ ');"'
				+ '><td><input type="checkbox" style="display: ;" name="merId" '
				+ ' merchandiseName=' + data[i].merchandiseName
				+ ' merchandiseNorm=' + data[i].merchandiseNorm
				+ ' merchandiseInPrice=' + data[i].merchandiseInPrice
				+ ' merchandiseTypeName=' + data[i].merchandiseTypeName
				+ ' merchandiseUnitName=' + data[i].merchandiseUnitName
				+ ' merchandiseAmount=' + data[i].merchandiseAmount
				+ ' merchandiseId=' + data[i].merchandiseId + ' value='
				+ data[i].merchandiseId + ' ></td><td>'
				+ data[i].merchandiseName + '</td><td>'
				+ data[i].merchandiseTypeName + '</td><td>'
				+ data[i].merchandiseUnitName + '</td><td>'
				+ data[i].merchandiseNorm + '</td><td>'
				+ data[i].merchandiseInPrice + '</td><td>'
				+ data[i].merchandiseAmount + '</td></tr>';
	}
	$("#merList").html(str);
}
function selectAllMer() {
	var url = '/stock/pages/bought.do?method=selectMer&d=' + new Date();
	$.getJSON(url, {}, callback);
}
function selectMer() {
	var merchandiseName = document.getElementById('smerName').value;
	var merchandiseNorm = document.getElementById('smerNorm').value;
	var url = '/stock/pages/bought.do?method=selectMer&d=' + new Date()
			+ '&merchandiseName=' + escape(escape(merchandiseName))
			+ '&merchandiseNorm=' + escape(escape(merchandiseNorm));
	$.getJSON(url, {}, callback);
}
function addOldMer() {
	var merBox = document.getElementsByName('merId');
	var mer;
	var str = '';
	for ( var i = 0; i < merBox.length; i++) {
		if (merBox.item(i).checked == true) {
			mer = merBox.item(i);
			var merchandiseId = mer.getAttribute('merchandiseId');
			var merchandiseName = mer.getAttribute('merchandiseName');
			var merchandiseNorm = mer.getAttribute('merchandiseNorm');
			var merchandiseInPrice = "上次进价："
					+ mer.getAttribute('merchandiseInPrice');
			var merchandiseTypeName = mer.getAttribute('merchandiseTypeName');
			var merchandiseUnitName = mer.getAttribute('merchandiseUnitName');
			var merchandiseAmount = "库存数量："
					+ mer.getAttribute('merchandiseAmount');
			var merchandiseOldAmount = mer.getAttribute('merchandiseAmount');
			var merchandiseTotalPrice = 0;
			str = str
					+ '<tr id="tr'+i+'"><td width="100px"><input id="'
					+ merchandiseId
					+ '" type="checkbox" style="display:none;" name="choseMers" '
					+ ' merchandiseInPrice='
					+ merchandiseInPrice
					+ ' merchandiseAmount='
					+ merchandiseAmount
					+ ' merchandiseOldAmount='
					+ merchandiseOldAmount
					+ ' merchandiseTotalPrice='
					+ merchandiseTotalPrice
					+ ' merchandiseId="'
					+ merchandiseId
					+ '" >'
					+ merchandiseName
					+ '</td><td width="50px">'
					+ merchandiseUnitName
					+ '</td><td width="80px">'
					+ merchandiseNorm
					+ '</td><td width="100px"><input style="width: 100px;" onclick="this.value=\'\'" onchange="getTotalPrice(\''
					+ merchandiseId
					+ '\');" type="text" id="'
					+ merchandiseId
					+ 'Price" value="'
					+ merchandiseInPrice
					+ '" />'
					+ '</td><td width="100px"><input style="width: 100px;" onclick="this.value=\'\'" onchange="getTotalPrice(\''
					+ merchandiseId
					+ '\');" type="text" id="'
					+ merchandiseId
					+ 'Amount" value="'
					+ merchandiseAmount
					+ '" />'
					+ '</td><td width="80px"><input style="width: 50px;" type="text" name="totalPrice" id="'
					+ merchandiseId
					+ 'TotalPrice" readonly="readonly" value="'
					+ merchandiseTotalPrice
					+ '" /></td>'
					+ '<td width="40px"><input type="button" value="删除" onclick="updateTable(\'tr'+i+'\')" /></td></tr>';
		}
	}
	hiddenDiv('mask');
	createTable(str);
}
function updateTable(trId) {
	var choseMerList = document.getElementById('choseMerList');
	var tr = document.getElementById(trId);
	choseMerList.removeChild(tr)
}
function createTable(str) {
	var choseMerList = document.getElementById('choseMerList');
	var choseMerListHtml = choseMerList.outerHTML;
	var htmlLength = choseMerListHtml.length;
	str = choseMerListHtml.substring(25, htmlLength - 8) + str;
	$("#choseMerList").html(str);
}
function getTotalPrice(merId) {
	var merPrice = merId + 'Price';
	var merAmount = merId + 'Amount';
	var merTotalPrice = merId + 'TotalPrice';
	var price = parseFloat(document.getElementById(merPrice).value).toFixed(2);
	var amount = parseInt(document.getElementById(merAmount).value);
	var totalePrice = price * amount;
	document.getElementById(merTotalPrice).value = totalePrice.toFixed(2);

	var mer = document.getElementById(merId);
	mer.setAttribute('merchandiseInPrice', price);
	mer.setAttribute('merchandiseAmount', amount);
	mer.setAttribute('merchandiseTotalPrice', totalePrice);

	var prices = document.getElementsByName('totalPrice');
	var totalPrice = 0;
	for ( var i = 0; i < prices.length; i++) {
		totalPrice = totalPrice + parseFloat(prices.item(i).value);
	}
	document.getElementById('inTotalPrice').value = totalPrice.toFixed(2);

}
function saveInvoice() {
	var inout = document.getElementById('inout').value;
	var storehouse = document.getElementById('storehouse').value;
	var tDate = document.getElementById('tDate').value;
	var inTotalPrice = document.getElementById('inTotalPrice').value;
	var buyer = document.getElementById('buyer').value;
	var invoiceCid = document.getElementById('invoiceCid').value;
	var memo = escape(escape(document.getElementById('memo').value));
	var merbox = document.getElementsByName('choseMers');
	var mer, merId, merPrice, merAmount, merTotalPrice;
	var size = merbox.length;
	var mers = new Array(size);
	for ( var i = 0; i < size; i++) {
		mer = merbox.item(i);
		merId = mer.getAttribute('merchandiseId');
		merPrice = mer.getAttribute('merchandiseInPrice');
		merAmount = mer.getAttribute('merchandiseAmount');
		merOldAmount = mer.getAttribute('merchandiseOldAmount');
		merTotalPrice = mer.getAttribute('merchandiseTotalPrice');
		var merArr = new Array(4);
		merArr[0] = merId;
		merArr[1] = parseFloat(merPrice).toFixed(2);
		merArr[2] = merAmount;
		merArr[3] = merOldAmount;
		merArr[4] = merTotalPrice;
		mers[i] = merArr;
	}
	var url = '/stock/pages/bought.do?method=createInvoice&d=' + new Date()
			+ '&inOutId=' + inout + '&storehouseId=' + storehouse
			+ '&invoiceDate=' + tDate + '&invoiceTotal=' + inTotalPrice
			+ '&invoiceMemo=' + memo + '&employeeId=' + buyer + '&invoiceCid='
			+ invoiceCid + '&mers=' + mers.toString();
	$.post(url, {}, function(data) {
		alert(data);
		location.reload();
	});
}